package com.example.recive;

import com.example.config.CommentMQConfig;
import com.example.service.appservice.AppForumService;
import com.example.vo.CommentVO;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;


/**
 * @BelongsProject："smart-property"
 * @BelongsPackage："com.example.recive"
 * @CreateTime："2022-11-21 10:05"
 * @Description：监听评论
 * @Author：LK
 */
@Log4j2
@Component
public class CommentMQRecive {

    @Autowired
    private AppForumService appForumService;

    @RabbitListener(queues = CommentMQConfig.SMART_QUEUE_NAME)
    public void reciveComment(Message message, Channel channel) throws IOException, ClassNotFoundException {
        log.info("接收到的信息：" + message.getBody());
        String s = new String(message.getBody());
        System.out.println(s);
        // 创建ByteArrayInputStream对象
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(message.getBody());
        // 创建ObjectInputStream对象
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        CommentVO commentVO = (CommentVO)objectInputStream.readObject();
        System.out.println(commentVO);
        // 持久化到数据库
        appForumService.sendComments(commentVO);


    }
}
